package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.ProductType;
import com.cloudfun.campusshare.common.model.vo.DeviceServiceTimeAddVO;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.common.response.BaseResultFactory;
import com.cloudfun.campusshare.common.response.PageResult;
import com.cloudfun.campusshare.common.security.AuthRequired;
import com.cloudfun.campusshare.service.DeviceServiceTimeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalTime;
import java.util.List;


/**
 * @author : zhangyongjie
 * @version : 1.0
 * @createTime : 2020/5/9 10:29
 * @description : 设备服务时间
 */
@Controller
@RequestMapping("/v1/device/serviceTime")
@Api(value = "设备服务时间")
@Validated
@Slf4j
@AuthRequired
public class DeviceServiceTimeController {

    @Autowired
    private DeviceServiceTimeService deviceServiceTimeService;

    @ApiOperation(value = "添加设备服务时间", notes = "添加设备服务时间")
    @PostMapping("/add")
    @ResponseBody
    public BaseResult addDeviceServiceTime(@RequestBody DeviceServiceTimeAddVO deviceServiceTimeAddVO) {
        deviceServiceTimeService.addDeviceServiceTime(deviceServiceTimeAddVO);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "查询设备服务时间列表", notes = "查询设备服务时间列表")
    @GetMapping("/list")
    @ResponseBody
    // TODO 包装成对象
    public BaseResult queryDeviceServiceTimeList(Pageable pageable,
                                                 @ApiParam(value = "关键字") @RequestParam(required = false) String keyword,
                                                 @ApiParam(value = "开始时间") @RequestParam(required = false) String startTime,
                                                 @ApiParam(value = "结束时间") @RequestParam(required = false) String endTime,
                                                 @ApiParam(value = "设备种类") @RequestParam(required = false) List<String> deviceType,
                                                 @ApiParam(value = "商家id") @RequestParam(required = false) String userId,
                                                 @ApiParam(value = "洗衣点id") @RequestParam(required = false) List<String> washPointId,
                                                 @ApiParam(value = "产品类型") @RequestParam(required = false) String productType) {
        PageResult pageResult = deviceServiceTimeService.queryDeviceServiceTimeList(pageable, keyword,
                StringUtils.isNotBlank(startTime) ? LocalTime.parse(startTime) : null, StringUtils.isNotBlank(endTime) ? LocalTime.parse(endTime) : null,
                deviceType, userId, washPointId, productType);
        return BaseResultFactory.produceSuccess(pageResult);
    }

    @ApiOperation(value = "删除设备服务时间列表", notes = "删除设备服务时间列表")
    @GetMapping("/delete/{deviceServiceTimeId}")
    @ResponseBody
    public BaseResult deleteDeviceServiceTime(@PathVariable String deviceServiceTimeId) {
        deviceServiceTimeService.deleteDeviceServiceTime(deviceServiceTimeId);
        return BaseResultFactory.produceSuccess();
    }

    @ApiOperation(value = "设备编辑查询设备服务时间列表", notes = "设备编辑查询设备服务时间列表")
    @GetMapping("/deviceEdit/list")
    @ResponseBody
    public BaseResult queryTimeListForDeviceEdit(@ApiParam(value = "设备种类") @RequestParam(required = false) String deviceType, @RequestParam(required = false)@ApiParam(value = "洗衣点id") String washPointId) {
        return BaseResultFactory.produceSuccess(deviceServiceTimeService.queryTimeList4DeviceEdit(deviceType, washPointId));
    }

}
